package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.BaseSignListResponse
import com.gexiaobao.pigeon.databinding.ItemGroupNameBinding

/**
 * @Author      : hxw
 * @Date        : 2024/7/16 15:22
 * @Describe    :
 */
class AdapterGroupName : BaseQuickAdapter<BaseSignListResponse.SignPigeonListBean.SignStatusBean, DataBindBaseViewHolder>(R.layout.item_group_name) {

    private var mOnGroupNameClickListener: OnGroupNameCheckClickListener? = null
    private val checkList = ArrayList<BaseSignListResponse.SignPigeonListBean.SignStatusBean>()

    override fun convert(holder: DataBindBaseViewHolder, item: BaseSignListResponse.SignPigeonListBean.SignStatusBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemGroupNameBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val isLIne = holder.layoutPosition == data.size - 1
        holder.setGone(R.id.viewLine, isLIne)

        val name = data[getItemPosition(item)]
        if (!name.isDisAble) {
            if (name.isCheck) {
                holder.setImageResource(R.id.ivItemChecked, R.mipmap.icon_sign_checked)
            } else {
                holder.setImageResource(R.id.ivItemChecked, R.mipmap.icon_sign_unchecked)
            }
        } else {
            holder.setImageResource(R.id.ivItemChecked, R.mipmap.icon_disable_group)
        }

        holder.itemView.setOnClickListener {
            if (data[getItemPosition(item)].isCheck) {
                name.isCheck = false
                notifyItemChanged(getItemPosition(item))
            } else {
                name.isCheck = true
                notifyItemChanged(getItemPosition(item))
            }
            checkList.clear()
            data.forEach {
                if (it.isCheck) {
                    checkList.add(it)
                } else {
                    checkList.remove(it)
                }
            }
            var sum = 0.0
            checkList.forEach {
                sum += it.money.toDouble()
            }
            mOnGroupNameClickListener?.onItemClick(getItemPosition(item), checkList, data, sum.toString())//data-返回原list，只改变状态值, checkList-返回修改过的list
        }
    }

    /**
     * 全选方法
     *
     * @return List<Fruit> 返回返选后的集合
     */
    @SuppressLint("NotifyDataSetChanged")
    fun setInvertSelection(): List<BaseSignListResponse.SignPigeonListBean.SignStatusBean> {
        data.forEach {
            if (!it.isCheck) {
                checkList.add(it)
            }
        }
        data.forEach {
            it.isCheck = true
        }
        notifyDataSetChanged()
        return checkList
    }

    /**
     * 清除所有所选
     */
    @SuppressLint("NotifyDataSetChanged")
    fun setClear() {
        data.forEach {
            if (it.isCheck) {
                it.isCheck = false
            }
        }
        notifyDataSetChanged()
    }

    fun setOnMultipleChoiceClickListener(listener: OnGroupNameCheckClickListener?) {
        mOnGroupNameClickListener = listener
    }

    interface OnGroupNameCheckClickListener {
        fun onItemClick(
            position: Int,
            checkList: ArrayList<BaseSignListResponse.SignPigeonListBean.SignStatusBean>,
            list: List<BaseSignListResponse.SignPigeonListBean.SignStatusBean>,
            sum: String
        )
    }

}